import 'package:flutter/material.dart';
import 'SettingsPage/SettingItem.dart';
import 'SettingsPage/RepairPlanPage.dart';
import 'SettingsPage/FeaturesPage.dart';
import 'SettingsPage/SettingMessagePage.dart';
import 'SettingsPage/ServicePage.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'LoginPage/LoginPage.dart';
import 'SettingsPage/SettingsSystemPage.dart';
import 'SettingsPage/settingsSub/FirmPage.dart';
import '../Tools/ToolsLocalStorage.dart';
import 'widgets/AppColors.dart';

class SettingsPage extends StatelessWidget {
  SettingsPage({super.key});
  static const array = ['通讯录', '服务中心', '我的消息', '设置'];
  static const imageArray = [
    'assets/images/setting/setting_1.png',
    'assets/images/setting/setting_3.png',
    'assets/images/setting/setting_4.png',
    'assets/images/setting/setting_6.png'
  ];
  final user = LocalStorage.getJson('user');
  var deptName = LocalStorage.getstr('deptName');
  Future<String?> getDeptName() async {
    return await LocalStorage.getstr('deptName');
  }

  get child => null;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.transparent,
        body: Stack(children: [
          Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: Image.asset(
              'assets/images/home/home_topbg.png',
              fit: BoxFit.cover,
              height: 400,
            ),
          ),
          // GestureDetector(
          //   onTap: () {},
          //   child: Image(
          //     image: AssetImage(AppColors.isDarkMode
          //         ? 'assets/images/home/nav_logo_d.png'
          //         : 'assets/images/home/nav_logo.png'),
          //     width: 76,
          //     height: 76,
          //   ),
          // ),
          ListView(children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Align(
                  alignment: Alignment.centerRight,
                  child: FutureBuilder<String?>(
                    future: getDeptName(), // 使用 FutureBuilder 来处理异步数据
                    builder: (context, snapshot) {
                      if (snapshot.connectionState == ConnectionState.waiting) {
                        return CircularProgressIndicator(); // 显示加载指示器
                      }
                      return GestureDetector(
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => const FirmPage()),
                          );
                        },
                        child: Container(
                          width: 208,
                          decoration: BoxDecoration(
                            color: const Color(0x4DFFFFFF),
                            borderRadius: BorderRadius.circular(20),
                          ),
                          margin: const EdgeInsets.only(right: 10, top: 10),
                          padding: const EdgeInsets.symmetric(
                              vertical: 4, horizontal: 16),
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Image(
                                  image: AssetImage(AppColors.isDarkMode
                                      ? 'assets/images/setting/check_b.png'
                                      : 'assets/images/setting/check.png')),
                              SizedBox(width: 8),
                              Flexible(
                                child: Text(
                                  snapshot.data ?? '未知部门', // 提供默认值
                                  overflow: TextOverflow.ellipsis,
                                  style:
                                      TextStyle(color: AppColors.textPrimary),
                                ),
                              ),
                            ],
                          ),
                        ),
                      );
                    },
                  ),
                ),
                SizedBox(
                    width: double.infinity,
                    child: Column(children: [
                      const SizedBox(height: 20),
                      // const Text(
                      //   '高级工程师-张工',
                      //   style: TextStyle(
                      //       fontSize: 18, fontWeight: FontWeight.bold),
                      // ),
                      FutureBuilder<Map<String, dynamic>>(
                        future: user,
                        builder: (context, snapshot) {
                          if (snapshot.hasData) {
                            return Text(
                              snapshot.data!['nickName'] ?? '',
                              style: TextStyle(
                                  fontSize: 18, fontWeight: FontWeight.bold),
                            );
                          }
                          return CircularProgressIndicator();
                        },
                      ),
                      const SizedBox(height: 10),
                      FutureBuilder<Map<String, dynamic>>(
                        future: user,
                        builder: (context, snapshot) {
                          if (snapshot.hasData) {
                            return TDImage(
                              imgUrl: 'http://8.137.93.51:8080' +
                                  snapshot.data!['avatar'],
                              type: TDImageType.circle,
                              width: 100,
                              height: 100,
                            );
                          }
                          return CircularProgressIndicator();
                        },
                      ),
                      // const CircleAvatar(
                      //   radius: 50,
                      //   backgroundImage:
                      //       AssetImage('assets/images/setting/user_logo.png'),
                      // ),
                      const SizedBox(height: 20),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          FutureBuilder<Map<String, dynamic>>(
                            future: user,
                            builder: (context, snapshot) {
                              if (snapshot.hasData) {
                                return Text(
                                  '电话 ${snapshot.data!['phonenumber']}' ?? '',
                                  style: TextStyle(
                                      fontSize: 15,
                                      color: AppColors.textSecondary),
                                );
                              }
                              return CircularProgressIndicator();
                            },
                          ),
                          FutureBuilder<Map<String, dynamic>>(
                            future: user,
                            builder: (context, snapshot) {
                              if (snapshot.hasData) {
                                return Text(
                                  snapshot.data!['post'] ?? '',
                                  style: TextStyle(
                                      fontSize: 15,
                                      color: AppColors.textSecondary),
                                );
                              }
                              return CircularProgressIndicator();
                            },
                          ),
                        ],
                      ),
                      const SizedBox(height: 20)
                    ])),
                Container(
                  margin: const EdgeInsets.symmetric(horizontal: 16),
                  decoration: BoxDecoration(
                    color: AppColors.cardColor,
                    borderRadius: BorderRadius.circular(12),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.grey.withOpacity(0.1),
                        spreadRadius: 1,
                        blurRadius: 3,
                        offset: const Offset(0, 1),
                      ),
                    ],
                  ),
                  child: ListView.builder(
                    itemCount: 4, // 数据条数
                    shrinkWrap: true, // 适应内容高度
                    physics: const NeverScrollableScrollPhysics(),
                    itemBuilder: (context, index) {
                      return Padding(
                        padding: const EdgeInsets.symmetric(
                            vertical: 0.0), // 设置行间距为1
                        child: GestureDetector(
                          // 添加点击事件
                          onTap: () {
                            if (index == 0) {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) =>
                                        const RepairPlanPage()),
                              );
                            } else if (index == 1) {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => const ServicePage()),
                              );
                            } else if (index == 2) {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) =>
                                        const SettingMessagePage()),
                              );
                            } else if (index == 3) {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) =>
                                        const SettingsSystemPage()),
                              );
                            }
                          },
                          child: SettingItem(
                              title: array[index], image: imageArray[index]),
                        ),
                      );
                    },
                  ),
                ),
              ],
            ),
          ])
        ]));
  }
}
